package zhang.lao.api.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import sun.nio.ch.ThreadPool;
import zhang.lao.api.testThread.UpLoadLatLng;
import zhang.lao.build.tool.CoordinateConversion;
import zhang.lao.build.tool.PathKit;
import zhang.lao.build.tool.Point;
import zhang.lao.pojo.console.resp.HttpResult;
import zhang.lao.pojo.console.resp.HttpResultUtil;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author
 * @create 2018-06-04 16:24
 **/
@Controller
@CrossOrigin("*")
public class TestAotuController {
    public static volatile boolean canRun = true;
    public static  Map<String,Boolean> machineMap = new HashedMap();
   private static final ExecutorService fixedThread = Executors.newFixedThreadPool(5);

    @RequestMapping(value = "/api/test/start/{deviceId}",method = RequestMethod.POST)
    public @ResponseBody HttpResult testStart(@PathVariable int deviceId) throws IOException {
        canRun = true;
        String fileName = PathKit.getWebRootPath()+ File.separator+"testtrace"+File.separator+deviceId+".json";


        byte[] bytes = Files.readAllBytes(Paths.get(fileName));
        String s = new String(bytes, Charset.forName("utf-8"));
        JSONArray jsonObject = JSON.parseArray(s);
                if(machineMap.get(deviceId+"")!=null&&machineMap.get(deviceId+"")){
                }else{
                    fixedThread.submit(new UpLoadLatLng(jsonObject,deviceId));
            }
        return HttpResultUtil.buildSuccess();
    }

    @RequestMapping(value = "/api/test/stop",method = RequestMethod.POST)
    public @ResponseBody HttpResult stop() throws IOException {
        canRun =false;
        return HttpResultUtil.buildSuccess();
    }

    @RequestMapping(value = "/api/test/upload",method = RequestMethod.POST)
    public @ResponseBody HttpResult upload(String deviceId,String deviceTrace) throws IOException {
        canRun =false;
        String content = "deviceTrace";
        String fileName = PathKit.getWebRootPath()+ File.separator+"testtrace"+File.separator+deviceId+".json";
        File file = new File(fileName);
        if(file.exists()){
            file.delete();
        }
        file.createNewFile();
        Files.write(Paths.get(fileName), deviceTrace.getBytes("UTF-8"));

        return HttpResultUtil.buildSuccess();
    }

    public static void main(String[] args) {
        initPoint("{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"properties\":{\"stroke\":\"#30417a\",\"stroke-width\":2,\"stroke-opacity\":1,\"id\":70001},\"geometry\":{\"type\":\"LineString\",\"coordinates\":[[116.6096591949463,40.22976699083938],[116.60963773727417,40.23006186776361],[116.60961627960206,40.2302011147538],[116.60961627960206,40.2302748336327],[116.60961627960206,40.2303649343757],[116.60962700843811,40.23061066306561],[116.60962700843811,40.23090553631664],[116.60962700843811,40.23124955348689],[116.60961627960206,40.231618141372984],[116.60959482192993,40.23197034570102],[116.6095733642578,40.23231435746244],[116.60958409309387,40.23261741399495],[116.60956263542174,40.23298599443651],[116.60959482192993,40.23319895111062],[116.60972356796263,40.23319895111062],[116.6098201274872,40.2332153323655],[116.61002397537231,40.233157997956056],[116.61003470420837,40.23266655816974],[116.61001324653624,40.23219968706946],[116.61006689071655,40.231667286272916],[116.61003470420837,40.231224980889806],[116.61006689071655,40.23078267261806],[116.61007761955261,40.23043046211352],[116.61009907722475,40.23033217048303],[116.61010980606079,40.23009463178707]]}},{\"type\":\"Feature\",\"properties\":{\"stroke\":\"#0d9d10\",\"stroke-width\":2,\"stroke-opacity\":1,\"id\":70002},\"geometry\":{\"type\":\"LineString\",\"coordinates\":[[116.61060333251953,40.23002910372433],[116.6105818748474,40.23018473276981],[116.61059260368347,40.23030759755311],[116.61060333251953,40.23038950728481],[116.6105818748474,40.23077448169691],[116.6105818748474,40.23124136262218],[116.6105282306671,40.23163452301025],[116.61054968833923,40.23199491802766],[116.6105282306671,40.23241264621618],[116.610506772995,40.232904087845085],[116.61048531532288,40.23320714173857],[116.61068916320801,40.233223522991466],[116.61080718040466,40.233248094863384],[116.6110110282898,40.23318256985177],[116.61100029945374,40.23295323181184],[116.61102175712584,40.23249455340199],[116.61098957061768,40.23196215492349],[116.61102175712584,40.23145432478238],[116.61106467247008,40.23075809985159],[116.61105394363402,40.23043865307629],[116.61104321479797,40.23035674340402],[116.61105394363402,40.23017654177632]]}},{\"type\":\"Feature\",\"properties\":{\"stroke\":\"#9c3d0e\",\"stroke-width\":2,\"stroke-opacity\":1,\"id\":70003},\"geometry\":{\"type\":\"LineString\",\"coordinates\":[[116.61160111427307,40.22999633966917],[116.61159038543703,40.23018473276981],[116.61155819892883,40.23029121559489],[116.61155819892883,40.23043865307629],[116.61155819892883,40.23065161776058],[116.61155819892883,40.23091372722196],[116.61152601242065,40.231192217413174],[116.61152601242065,40.23158537808656],[116.61149382591248,40.23218330556891],[116.61146163940428,40.23263379539051],[116.611407995224,40.23287951584834],[116.61139726638794,40.23312523541459],[116.61139726638794,40.23328085734542],[116.61156892776489,40.23328085734542],[116.61172986030579,40.233289047963446],[116.61188006401062,40.23328085734542],[116.61190152168273,40.23311704477674],[116.61195516586305,40.2328303718281],[116.61190152168273,40.23245359982146],[116.61190152168273,40.23206863495403],[116.61190152168273,40.231716431137144],[116.61192297935486,40.23142975225962],[116.61192297935486,40.231036590682784],[116.61194443702696,40.23071714522102],[116.61194443702696,40.23047141691746],[116.61192297935486,40.230381316316105],[116.61195516586305,40.23023387870988],[116.61195516586305,40.23009463178707]]}}]}");
        initPoint("{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\",\"properties\":{\"stroke\":\"#3c6f40\",\"stroke-width\":2,\"stroke-opacity\":1,\"id\":70004},\"geometry\":{\"type\":\"LineString\",\"coordinates\":[[116.60951972007751,40.22999633966917],[116.60953044891356,40.230135586794084],[116.6095519065857,40.23023387870988],[116.6095519065857,40.23029121559489],[116.60956263542174,40.23038950728481],[116.6095733642578,40.230856390863956],[116.60950899124146,40.231192217413174],[116.60948753356932,40.23157718726248],[116.60945534706117,40.23194577336548],[116.60947680473328,40.23238807404112],[116.60946607589722,40.23314980732218],[116.6095733642578,40.23314980732218],[116.60968065261841,40.23314980732218],[116.60977721214296,40.233141616687305],[116.60977721214296,40.233018757045365],[116.609787940979,40.23269932093311],[116.609787940979,40.23245359982146],[116.60979866981506,40.2320604441884],[116.60979866981506,40.2317000495197],[116.60979866981506,40.23126593521331],[116.60983085632324,40.230921918126285],[116.60986304283142,40.230684381498726],[116.60985231399535,40.230414080185014],[116.60995960235596,40.230397698252524],[116.61010980606079,40.230397698252524],[116.61014199256898,40.23060247212363],[116.61014199256898,40.23082362720901],[116.61014199256898,40.23109392688823],[116.61014199256898,40.23151985146616],[116.61014199256898,40.23201129957378],[116.6101312637329,40.23230616672653],[116.61010980606079,40.23266655816974],[116.61006689071655,40.23305971028412],[116.61006689071655,40.23320714173857],[116.61015272140503,40.233223522991466],[116.61027073860168,40.23320714173857],[116.61037802696227,40.23320714173857],[116.61038875579834,40.2330023757429],[116.61038875579834,40.23274027436504],[116.6103994846344,40.23242902766126],[116.6103994846344,40.23209320724499],[116.6103994846344,40.23180652996235],[116.61043167114258,40.23155261478429],[116.61043167114258,40.231192217413174],[116.61043167114258,40.23083181812425],[116.61045312881468,40.23046322595866],[116.61045312881468,40.230381316316105],[116.61073207855223,40.23038950728481],[116.61077499389647,40.230577899291795],[116.61075353622437,40.23080724537562],[116.61075353622437,40.23108573600471],[116.61072134971619,40.23143794310153],[116.61068916320801,40.23176557596575],[116.61068916320801,40.232076825718664],[116.61068916320801,40.2324454091024],[116.61069989204405,40.23271570230885],[116.61069989204405,40.23297780378182],[116.61067843437195,40.2332399042404],[116.61066770553589,40.23344466951782]]}},{\"type\":\"Feature\",\"properties\":{\"stroke\":\"#555555\",\"stroke-width\":2,\"stroke-opacity\":1,\"id\":70005},\"geometry\":{\"type\":\"LineString\",\"coordinates\":[[116.61109685897826,40.22997995763566],[116.61107540130615,40.230135586794084],[116.61107540130615,40.23026664265011],[116.61107540130615,40.23031578853074],[116.61107540130615,40.23044684403807],[116.61105394363402,40.23059428118068],[116.61104321479797,40.23096287263304],[116.61104321479797,40.23136422548865],[116.61103248596191,40.23179833916502],[116.61093592643736,40.232166924064394],[116.61093592643736,40.23259284189421],[116.61093592643736,40.232887706514916],[116.6109573841095,40.233141616687305],[116.61094665527345,40.233289047963446],[116.61102175712584,40.233289047963446],[116.61112904548645,40.23328085734542],[116.61131143569946,40.23328085734542],[116.61130070686339,40.23308428221547],[116.61127924919128,40.2328385625006],[116.61125779151915,40.23259284189421],[116.61125779151915,40.23229797598963],[116.61125779151915,40.23201129957378],[116.61127924919128,40.23156899643741],[116.61128997802734,40.2310775451202],[116.61132216453552,40.23055332645103],[116.61130070686339,40.23043046211352],[116.611407995224,40.230414080185014],[116.61156892776489,40.23042227114975],[116.61163330078126,40.230659808696615],[116.61163330078126,40.23096287263304],[116.61160111427307,40.23146251562132],[116.61160111427307,40.23185567472554],[116.61154747009276,40.23234712039622],[116.6115367412567,40.23270751162149],[116.61154747009276,40.233108854137924],[116.6115152835846,40.23328085734542],[116.61163330078126,40.233297238580505],[116.61171913146973,40.2332726667264],[116.61183714866638,40.2332399042404],[116.61184787750244,40.23296142246949],[116.61186933517455,40.23259284189421],[116.61185860633849,40.23215873331065],[116.61186933517455,40.23168366789827],[116.61185860633849,40.23121679002214],[116.61189079284668,40.23088096359475],[116.61192297935486,40.23051237169662],[116.61193370819092,40.23043046211352],[116.61199808120728,40.23045503499886],[116.61211609840394,40.23045503499886],[116.61210536956786,40.2305758515554],[116.612129509449,40.23069257243079],[116.61212414503098,40.230889154503046],[116.61212682723999,40.231128738132135],[116.61211073398589,40.23132122351073],[116.6120919585228,40.23155875790467],[116.61208391189575,40.23187000860809],[116.61206245422362,40.23216897175267],[116.61205172538757,40.23244336142247],[116.6120383143425,40.23286108684493],[116.61203294992448,40.23314366434612],[116.61204099655151,40.233274714381245],[116.61201953887938,40.23335047756711],[116.6120222210884,40.23346514601153]]}}]}");
    }

    public static void initPoint(String json){
        JSONObject jsonObject = JSON.parseObject(json);
        JSONArray features = jsonObject.getJSONArray("features");
        for (Object feature : features) {
            JSONObject feature1 = (JSONObject) feature;
            JSONObject geometry = feature1.getJSONObject("geometry");
            JSONObject properties = feature1.getJSONObject("properties");
            Integer id = properties.getInteger("id");
            System.out.println(id);
            JSONArray jsonArray = new JSONArray();

            JSONArray coordinates = geometry.getJSONArray("coordinates");
            for (Object coordinate : coordinates) {
                JSONArray j = (JSONArray) coordinate;

                JSONObject jsonObject1 = new JSONObject();
                jsonObject1.put("lat",j.getDouble(1));
                jsonObject1.put("lon",j.getDouble(0));
                jsonArray.add(jsonObject1);
            }
            System.out.println(JSON.toJSONString(jsonArray));
        }
    }
}
